//初始化用户表
var userlist = [{
		id: '1',
		name: '张三',
		no: '10001',
		pwd: '0000',
		state: '1',
		zhiwu: '高级技工',
		phone: '15001234567',
	}, {
		id: '2',
		name: '李四',
		no: '10002',
		pwd: '0000',
		state: '1',
		zhiwu: '高级技工',
		phone: '15001234567',
	},
	{
		id: '3',
		name: '王五',
		no: '10003',
		pwd: '0000',
		state: '1',
		zhiwu: '高级技工',
		phone: '15001234568',
	},
	{
		id: '4',
		name: '刘红',
		no: '10004',
		pwd: '0000',
		state: '1',
		zhiwu: '高级技工',
		phone: '13500993322',
	}
];

//已下载工单表   状态 0待维修,1进行中,2已维修
var worder_list = [{
	id: '1',
	orderno: 'GD1001',
	name: '李女士',
	addr: '三单元502',
	phone: '15100094440',
	content: '厨房漏水',
	cdate: '2019-12-12',
	state: '0'
}, {
	id: '2',
	orderno: 'GD1002',
	name: '王女士',
	addr: '三单元105',
	phone: '13800445678',
	content: '停电',
	cdate: '2019-12-01',
	state: '0'
}, {
	id: '3',
	orderno: 'GD1003',
	name: '李先森',
	addr: '三单元102',
	phone: '15200094440',
	content: '停水',
	cdate: '2019-12-22',
	state: '0'
}, {
	id: '4',
	orderno: 'GD1004',
	name: '张先生',
	addr: '三1单元602',
	phone: '15200094440',
	content: '家里漏水了停电闷坏了赶紧来检测一下',
	cdate: '2019-12-30',
	state: '0'
}, {
	id: '5',
	orderno: 'GD1005',
	name: '武先生',
	addr: '1号楼一单元602',
	phone: '15200094440',
	content: '厨房漏水',
	cdate: '2019-12-30',
	state: '0'
}, {
	id: '6',
	orderno: 'GD1006',
	name: '刘女士',
	addr: '1号楼2单元601',
	phone: '15200094440',
	content: '门坏了',
	cdate: '2019-12-30',
	state: '0'
}, {
	id: '7',
	orderno: 'GD1007',
	name: '李开复',
	addr: '1号楼2单元601',
	phone: '15200094440',
	content: '门坏了',
	cdate: '2019-12-30',
	state: '0'
}, {
	id: '8',
	orderno: 'GD1008',
	name: '诸葛卧龙',
	addr: '11号楼3单元301',
	phone: '15200094440',
	content: '门坏了',
	cdate: '2019-12-30',
	state: '0'
}, {
	id: '9',
	orderno: 'GD1009',
	name: '百晓生',
	addr: '2号楼2单元601',
	phone: '15200094440',
	content: '1123门坏了',
	cdate: '2019-12-30',
	state: '0'
}];

//工单维修记录表
var worder_record = [
	// {
	// 	id: '1',
	// 	orderno: 'GD2001',
	// 	name: '王女士',
	// 	addr: '三单元602',
	// 	phone: '15109843333',
	// 	content: '厨房漏水',
	// 	cdate: '2020-01-01',
	// 	local_before_img:'',
	// 	local_after_img:'',
	// 	wx_before_img: '',
	// 	wx_after_img: '',
	// 	wx_content:'',
	// 	state: '0'
	// }, {
	// 	id: '2',
	// 	orderno: 'GD2002',
	// 	name: '李女士',
	// 	addr: '二单元102',
	// 	phone: '15109844444',
	// 	content: '门锁坏了',
	// 	cdate: '2020-01-01',
	// 	local_before_img:'',
	// 	local_after_img:'',
	// 	wx_before_img: '',
	// 	wx_after_img: '',
	// 	wx_content:'',
	// 	state: '1'
	// },
	// {
	// 	id: '3',
	// 	orderno: 'GD2012',
	// 	name: '苗女士',
	// 	addr: '4号楼二单元202',
	// 	phone: '15109844444',
	// 	content: '门锁坏了',
	// 	cdate: '2020-01-01',
	// 	local_before_img:'',
	// 	local_after_img:'',
	// 	wx_before_img: '',
	// 	wx_after_img: '',
	// 	wx_content:'',
	// 	state: '0'
	// }
]

//创建数据库表
function opendb() {
	plus.sqlite.openDatabase({
		name: 'test',
		path: '_doc/test.db',
		success: function(e) {
			console.log('openDatabase success!');
		},
		fail: function(e) {
			console.log('openDatabase failed: ' + JSON.stringify(e));
		}
	});
}
//关闭数据库
function closedb() {
	plus.sqlite.closeDatabase({
		name: 'test',
		success: function(e) {
			console.log('closeDatabase success!');
		},
		fail: function(e) {
			console.log('closeDatabase failed: ' + JSON.stringify(e));
		}
	});
}
//查询数据
function searchdb() {
	plus.sqlite.selectSql({
		name: 'test',
		sql: 'select * from database',
		success: function(data) {
			console.log('selectSql success: ');
			for (var i in data) {
				console.log(data[i]);
			}
		},
		fail: function(e) {
			console.log('selectSql failed: ' + JSON.stringify(e));
		}
	});
}


//执行sql语句  -建表语句
function executesql() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: 'create table if not exists database("where" CHAR(110),"location" CHAR(100),"age" INT(11))',
		success: function(e) {
			console.log('executeSql success!');
			plus.sqlite.executeSql({
				name: 'test',
				sql: "insert into database values('北京','安乐林','11')",
				success: function(e) {
					console.log('executeSql success!');
				},
				fail: function(e) {
					console.log('executeSql failed: ' + JSON.stringify(e));
				}
			});
		},
		fail: function(e) {
			console.log('executeSql failed: ' + JSON.stringify(e));
		}
	});
}

//创建用户表
function executesql_user() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: 'create table if not exists c_user("id" varchar(100) ,"name" varchar(100),"no" INT(11),"pwd" varchar(12),"state" char(1),"zhiwu" varchar(50),"phone" varchar(50))',
		success: function(e) {
			//初始化用户表
			var sqls = '';
			for (var i = 0; i < userlist.length; i++) {
				var user = userlist[i];
				sqls = "insert into c_user values('" + user.id + "','" + user.name + "','" + user.no + "','" + user.pwd + "','" +
					user.state + "','" + user.zhiwu + "','" + user.phone + "');";
				plus.sqlite.executeSql({
					name: 'test',
					sql: sqls,
					success: function(e) {
						//console.log('executeSql success!');
					},
					fail: function(e) {
						console.log('executeSql failed: ' + JSON.stringify(e));
					}
				});
			}
			console.log('创建用户表 success!');
		},
		fail: function(e) {
			console.log('executeSql failed: ' + JSON.stringify(e));
		}
	});
}

//清空用户表
function truncate_user() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: "delete from c_user",
		success: function(e) {
			console.log('清空用户表 success!');
		},
		fail: function(e) {
			console.log('清空用户表 failed: ' + JSON.stringify(e));
		}
	});
}

//创建工单表
function executesql_workorder() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: 'create table if not exists c_wordorder("id" varchar(100) ,"orderno" varchar(50),"name" varchar(100),"addr" varchar(50),"phone" varchar(50),"content" varchar(255),"cdate" varchar(20),"state" char(1),"wx_people" varchar(20))',
		success: function(e) {
			//初始化下载工单
			var sqls = '';
			for (var i = 0; i < worder_list.length; i++) {
				var order = worder_list[i];
				sqls = "insert into c_wordorder values('" + order.id + "','" + order.orderno + "','" + order.name + "','" +
					order
					.addr + "','" +
					order.phone + "','" + order.content + "','" + order.cdate + "','" + order.state + "','" + "" + "');";
				plus.sqlite.executeSql({
					name: 'test',
					sql: sqls,
					success: function(e) {

					},
					fail: function(e) {
						console.log('工单表新建 failed: ' + JSON.stringify(e));
					}
				});
			}
			console.log('工单表新建 success!');
		},
		fail: function(e) {
			console.log('executeSql failed: ' + JSON.stringify(e));
		}
	});
}

//清空工单表
function truncate_workorder() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: "delete from c_wordorder",
		success: function(e) {
			console.log('清空工单表 success!');
		},
		fail: function(e) {
			console.log('清空工单表 failed: ' + JSON.stringify(e));
		}
	});
}
//创建维修记录表
function executesql_order_record() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: 'create table if not exists c_wordorder_record("id" varchar(100) ,"orderno" varchar(50),"addr" varchar(50),"name" varchar(100),"phone" varchar(50),"content" varchar(255),"cdate" varchar(20),"local_before_img" text,"local_after_img" text,"wx_before_img" text,"wx_after_img" text,"wx_content" varchar(255),"state" char(1),"wx_cdate" varchar(20),"wx_people" varchar(20))',
		success: function(e) {
			//初始化下载工单
			var sqls = '';
			for (var i = 0; i < worder_record.length; i++) {
				var order = worder_record[i];
				sqls = "insert into c_wordorder_record values('" + order.id + "','" + order.orderno + "','" + order.addr +
					"','" +
					order.name + "','" +
					order.phone + "','" + order.content + "','" + order.cdate + "','" + order.local_before_img + "','"+order.local_after_img + "','"+ order.wx_before_img + "','" + order.wx_after_img +
					  "','" + order.wx_content + "','" + order.state+ "','" + "" + "','" + ""+"');"; 
					plus.sqlite.executeSql({
						name: 'test',
						sql: sqls,
						success: function(e) {
							
						},
						fail: function(e) {
							console.log('创建维修记录表 failed: ' + JSON.stringify(e));
						}
					});
			}
			console.log('创建维修记录表 success!');
		},
		fail: function(e) {
			console.log('executeSql failed: ' + JSON.stringify(e));
		}
	});
}
//清空维修记录表
function truncate_order_record() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: "delete from c_wordorder_record",
		success: function(e) {
			console.log('清空维修记录表 success!');
		},
		fail: function(e) {
			console.log('清空维修记录表 failed: ' + JSON.stringify(e));
		}
	});
}

export default {
	opendb,
	closedb,
	executesql_user,
	executesql_workorder,
	truncate_workorder,
	truncate_user,
	executesql_order_record,
	truncate_order_record,
}
